package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time

object Demo1TimeWindow {

  def main(args: Array[String]): Unit = {

    //创建flink的运行环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val ds: DataStream[String] = env.socketTextStream("master", 7777)


    /**
      * TumblingProcessingTimeWindows ：处理时间滚动窗口
      * TumblingEventTimeWindows: 事件时间滚动窗口
      *
      * SlidingEventTimeWindows:事件时间滑动窗口
      * SlidingProcessingTimeWindows： 处理时间的滑动窗口
      *
      *
      *
      */
    ds.flatMap(_.split(","))
      .map((_, 1))
      .keyBy(_._1)
      //.timeWindow(Time.seconds(10), Time.seconds(2))
      .window(SlidingProcessingTimeWindows.of(Time.seconds(10), Time.seconds(2)))
      .sum(1)
      .print()


    env.execute()

  }
}
